<%@page import="com.cn.bulu.utils.RSAUtils"%>
<%@page import="com.cn.bulu.utils.Constant"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.security.interfaces.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
	name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>注册</title>
<link rel="stylesheet" href="${ctxPath }/static/customer/css/reg.css">
<style>
.row  .telVal {
	width: 43%;
}

.regForm .row {
	margin: 0.05rem 0;
}

@media screen and (max-width: 321px) {
	.row .inpVal {
		width: 37%;
	}
	.sendNumBtn {
		width: 30%;
		display:inline-block;
	}
}
.hide{
display: none;
}
</style>
<script src="${ctxPath }/static/customer/js/jquery-1.8.3.min.js"></script>
<script src="${ctxPath }/static/customer/layer/mobile/layer.js"></script>
<%
	HashMap<String, Object> map = RSAUtils.getKeys();
	//生成公钥和私钥    
	RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
	RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
	session.setAttribute("privateKey", privateKey);//私钥保存在session中，用于解密  
	//公钥信息保存在页面，用于加密  
	String publicKeyExponent = publicKey.getPublicExponent().toString(16); //公钥指数   
	String publicKeyModulus = publicKey.getModulus().toString(16); //模    
	request.setAttribute("publicKeyExponent", publicKeyExponent);
	request.setAttribute("publicKeyModulus", publicKeyModulus);
%>
</head>
<body>
	<div class="container">
		<div class="content">
			<form class="regForm">
				<!--电话-->
				<div class="row">
					<label for="phone" class="regLable" name="phone"><span
						style="color: #f3f3f3;">话话</span>电话：</label> 
						<input type="text"
						name="phone" class="inpVal telVal" placeholder="请输入手机号"><span
						unselectable="on" readonly class="sendNumBtn">获取验证码</span>
				</div>
				<!--验证码-->
				<div class="row">
					<label for="code" class="regLable" name="code"><span
						style="color: #f3f3f3;">话</span>验证码：</label> <input type="text"
						name="code" class="inpVal codeVal telVal" placeholder="请输入验证码">
					<input type="hidden" class="customerId">
				</div>
				<!--设置密码-->
				<div class="hide"  id="register_div">
					<div class="row">
						<label for="password" class="regLable" name="password">设置密码：</label>
						<input type="password" name="password1"
							class="inpVal telVal passVal" placeholder="请输入密码">
					</div>
					<!--确认密码-->
					<div class="row">
						<label for="password" class="regLable" name="password">确认密码：</label>
						<input type="password" name="password2"
							class="inpVal telVal passVal1" placeholder="请确认密码">
					</div>
					<!--昵称-->
					<div class="row">
						<label for="name" class="regLable" name="code"><span
							style="color: #f3f3f3;">话话</span>昵称：</label> <input type="text"
							name="nike" class="inpVal telVal nameVal" placeholder="请输入昵称">
					</div>
				</div>
				<div class="row">
					<input type="button" class="regBtn" value="确    定">
				</div>
			</form>
		</div>
	</div>
	<script src="${ctxPath }/static/customer/security/security.js"></script>
	<script>
    var countdown=60;
    var timer = null;
    var fn;
    function settime(obj) {
    	var $obj =$(obj) ,
    	newObj = $obj.hide().clone().css({display:"inline-block",width:""}).show().text("60s").insertAfter($obj);
    	var t = setInterval(function() {
    		var s = newObj.text().replace("s","") - 1;
    		if(s < 0) {
    			clearInterval(t);
    			newObj.remove();
    			$obj.show();
    			return;
    		}
    		newObj.text(s + "s");
    	}, 1000);
    }
    
	$(".sendNumBtn").click(function(){
        if($("input[name='phone']").val() !== ""){
            var re = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
            var tel = $("input[name='phone']").val();   
            var retu=re.test(tel);
            if(!retu){
                layer.open({
                    content: '请输入正确格式的手机号'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("input[name='phone']").val("");
                return false;
            }else{
            	//判断手机号是否已经注册
            		var btnTime=this;
            	   	$.post(
				  			"${ctxPath }/customer/isRegisted",
				  			{phone:$("input[name='phone']").val()},
				  			function(data){
				  				var status;
					   			var data2 = JSON.parse(data);
					   			//0：未绑定  1：你已经是平台的用户 2：还未注册鹿客家居
					   			 status=data2.status;
					   			if(status == 1 ){
					   				layer.open({
					   		           content: data2.message
					   		           ,skin: 'msg'
					   		           ,time: 2 //2秒后自动关闭
					   		       });
					   			  return;
					   			}else if(status == 2){
					   				//先注册
					   				$("#register_div").removeClass("hide");
					   				$(".regBtn").attr("value","注	   册");
					   				//继续执行
					   			//发送验证码
					                $.post("${ctxPath }/upload/sendMaByPhone",{sendType:4,phone:tel},function(data){
					                },"json");
					                settime(btnTime);
					   			}else{
					   				//继续执行
					   			//发送验证码
					                $.post("${ctxPath }/upload/sendMaByPhone",{sendType:4,phone:tel},function(data){
					                },"json");
					                settime(btnTime);
					   			}
				  			}
				   	);
            }
        }else{
            layer.open({
                content: '请输入手机号'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }
    })


    /*点击注册验证手机号*/

    //var reg = /(1[3-9]\d{9}$)/;
    //var re=new RegExp(/^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/);
	
     var pwd='<%=Constant.ENCRYPTKEY%>';
     //加密
      function Encrypt(word){
          return CryptoJS.AES.encrypt(word,pwd).toString();
      }
	//解密
      function Decrypt(word){
          return CryptoJS.AES.decrypt(word,pwd).toString(CryptoJS.enc.Utf8);
      }
    $(".regBtn").click(function(){
        if($("input[name='phone']").val() == ""){
            layer.open({
                content: '请输入手机号'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
            return false;
        }
        var re = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
    	var tel = $("input[name='phone']").val();
    	var retu=re.test(tel);
    	if(!retu){
             //alert("请输入正确格式的手机号");
             layer.open({
                 content: '请输入正确格式的手机号'
                 ,skin: 'msg'
                 ,time: 2 //2秒后自动关闭
             });
             return false;
       }
        if($("input[name='code']").val() == ""){
            layer.open({
                content: '请输入验证码'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
            return false;
        }
        //密码框
        if(! $("#register_div").hasClass("hide")){
        	if($(".passVal").val()==""){
        	    layer.open({
                    content: '请输入密码'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                return false;
        	}
        	if($(".passVal1").val()==""){
        	    layer.open({
                    content: '请再次确认密码'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                return false;
        	}
        	if($(".passVal").val() != $(".passVal1").val()){
        		 layer.open({
                     content: '请确认两次密码一致'
                     ,skin: 'msg'
                     ,time: 2 //2秒后自动关闭
                 });
                 return false;
        	}
        }
        //0:顾客扫码确认2:经销商扫码注册3推广员扫码注册 4顾客扫码下载APP
    	var identity=	"${identity }";
    	var customerId="${customerId }";
    	var url="";
    	if(identity=='0'){
    		url='${ctxPath }/customer/customerRefer';
    	}else{
    		 url='${ctxPath }/customer/subordinate';
    	}
    	if(!url){
    		return false;
    	}
        var codeNum = $("input[name='code']").val();
        var nickName="";
        var encrypedPwd="";
        if(! $("#register_div").hasClass("hide")){
        	nickName=$("input[name='nike']").val();
        	var password=$(".passVal1").val();
        	password = password.split("").reverse().join("");
        	RSAUtils.setMaxDigits(256);
    		var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "",
    				"${publicKeyModulus}");//公钥
    		encrypedPwd = RSAUtils.encryptedString(key, password);
        }
        $.post(
        	url,
        	{
        		customerId:customerId,
        		nickName:nickName,
        		phone:tel,
        		vertifyCode:codeNum,
        		passwd:encrypedPwd,
        		identity:identity
        	},
        	function(data){
	        	var data = JSON.parse(data);
	            if(data.status == 0){
		                layer.open({
		                    content: data.message
		                    ,skin: 'msg'
		                    ,time:5//2秒后自动关闭
		                });
		                //跳转到app下载页面
		               location.href='http://fir.im/lukejiaju';
		            }else{
		            	layer.open({
		                    content: data.message
		                    ,skin: 'msg'
		                    ,time:5//2秒后自动关闭
		                });
		            }
        })
        $(this).attr("disabled","true");
        //console.log($(".regLable").attr("number"));
        //lalert("222");
        setTimeout('$(".regBtn").removeAttr("disabled")',3000);
        return true;
    })
    </script>
</body>
</html>